<!-- Copyright 2013 Google Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// -->
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Unit Test of e2e.Cipher.RSA</title>
<script src="../../../../../javascript/closure/base.js"></script>
<script src="test_js_deps-runfiles.js"></script>
<script>
  goog.require('e2e.cipher.Rsa');
  goog.require('e2e.hash.Algorithm');
  goog.require('goog.array');
  goog.require('goog.testing.jsunit');
</script>
<script>
//<![CDATA[
function testBasic() {
  var n = [0xa1, 0xd3, 0xde, 0x96, 0xbb, 0x1c, 0x21, 0x52,
           0x83, 0xda, 0x66, 0xfa, 0xb9, 0x6a, 0x09, 0x47,
           0x73, 0x5e, 0xb7, 0x95, 0xd0, 0x8a, 0xf5, 0xe2,
           0xcb, 0x6b, 0x6d, 0xba, 0xc7, 0xe5, 0x6b, 0x2d,
           0x34, 0x57, 0x98, 0xa6, 0xfd, 0xd8, 0x40, 0x11,
           0xa5, 0x53, 0xa6, 0xb8, 0x40, 0x89, 0x0e, 0x4e,
           0x16, 0xae, 0xfe, 0x65, 0xa0, 0x2a, 0xc6, 0x19,
           0x4d, 0x81, 0x79, 0x88, 0xd6, 0x90, 0x00, 0x2e,
           0x77, 0x08, 0xf8, 0x8a, 0x18, 0xa0, 0xbc, 0xe4,
           0xc7, 0x66, 0x72, 0x0d, 0xaa, 0xb1, 0x06, 0xe1,
           0xbd, 0x52, 0xe5, 0x03, 0xb8, 0x25, 0x86, 0xf0,
           0xcc, 0xdc, 0x6a, 0xaf, 0x87, 0xc1, 0x54, 0x10,
           0xbb, 0x15, 0x34, 0xd2, 0x8e, 0x13, 0x88, 0x2e,
           0x47, 0xa9, 0x0b, 0x5f, 0x0d, 0x79, 0x1f, 0x22,
           0xaa, 0xef, 0x3a, 0x79, 0x31, 0x37, 0x62, 0x48,
           0x89, 0x84, 0x42, 0x8c, 0xd4, 0xd8, 0xbd, 0xe3];
  var e = [0x1, 0x00, 0x01];
  var d = [0x72, 0xa6, 0x67, 0x59, 0xc4, 0xe0, 0x8e, 0x1f,
           0x26, 0xb9, 0x32, 0x22, 0x4a, 0x00, 0x7c, 0xe8,
           0x79, 0x2d, 0x4e, 0xc5, 0xbe, 0x44, 0xe5, 0x25,
           0xb3, 0xd6, 0xbd, 0x9c, 0x0a, 0x09, 0x58, 0xe0,
           0x62, 0xe7, 0xf8, 0x87, 0xbe, 0xea, 0x59, 0xa1,
           0xe6, 0xce, 0x98, 0x1c, 0x8b, 0xe4, 0x4e, 0x15,
           0x36, 0xee, 0xed, 0xdf, 0x54, 0x15, 0x59, 0xda,
           0x0d, 0x56, 0x40, 0xa3, 0x98, 0xd7, 0xd6, 0xb7,
           0xf0, 0xf8, 0x5a, 0x54, 0xf0, 0x8d, 0x99, 0x80,
           0x94, 0x44, 0xd6, 0xb7, 0xf1, 0x3c, 0x2d, 0x7f,
           0xfd, 0x65, 0x3f, 0x0a, 0x09, 0x13, 0x2a, 0x2e,
           0x34, 0x0b, 0x52, 0x01, 0x7f, 0x95, 0x0d, 0x4f,
           0xca, 0xfa, 0x5b, 0xb1, 0xbc, 0x28, 0xd3, 0xa2,
           0x37, 0x0c, 0x93, 0xda, 0x77, 0xb7, 0x64, 0x1d,
           0xbc, 0xa0, 0x53, 0x61, 0x20, 0xcc, 0xeb, 0x2d,
           0xad, 0x16, 0x37, 0x1d, 0x33, 0xe9, 0xba, 0xd1];

  var plaintext = [0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,
                   0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,
                   0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02];
  var rsa = new e2e.cipher.Rsa(
    e2e.cipher.Algorithm.RSA,
    {n: n, e: e, d: d});
  assertEquals(e2e.hash.Algorithm.SHA1, rsa.getHashAlgorithm());
  var ciphertext = e2e.async.Result.getValue(rsa.encrypt(plaintext))['c'];
  assertArrayEquals(
    [0x90, 0x91, 0x9b, 0xc6, 0xa2, 0xce, 0x09, 0x75,
     0xf0, 0xb7, 0x7b, 0x30, 0x6b, 0xcd, 0x03, 0x96,
     0x4b, 0xfa, 0xd9, 0xec, 0x74, 0x17, 0xcc, 0x3d,
     0x28, 0x9a, 0xcc, 0xc1, 0x10, 0xf8, 0x3d, 0x18,
     0xc8, 0xf0, 0xb6, 0x70, 0x1d, 0xd6, 0x1e, 0x23,
     0xb1, 0xf4, 0x4d, 0x77, 0x8b, 0xd3, 0x3e, 0x37,
     0x06, 0xa9, 0x59, 0xc5, 0x03, 0x41, 0x10, 0xdf,
     0x61, 0x11, 0x48, 0xdd, 0x93, 0xc0, 0xfa, 0xa9,
     0x2a, 0xda, 0x62, 0x10, 0x84, 0x25, 0x65, 0xb9,
     0x15, 0x82, 0x39, 0xd7, 0x60, 0x58, 0x67, 0x10,
     0x72, 0x22, 0x18, 0x93, 0x39, 0xa2, 0x88, 0x89,
     0x18, 0x4c, 0x02, 0xc3, 0x0f, 0x23, 0xe3, 0x61,
     0xa5, 0x6d, 0x79, 0xc6, 0x36, 0x4e, 0x9a, 0xce,
     0x2e, 0x14, 0x21, 0xa9, 0x9b, 0x4c, 0x28, 0x2a,
     0xbc, 0xe4, 0x3e, 0x41, 0xd4, 0x79, 0x65, 0xde,
     0xcf, 0x89, 0xc8, 0xa0, 0x34, 0x9c, 0x0e, 0xbf], ciphertext);

  var recovered = e2e.async.Result.getValue(rsa.decrypt({c:ciphertext}));
  assertArrayEquals(plaintext, recovered);
};

// Allow for some leeway in checking for key sizes, so we allow upto
// 1020 bits for the modulus.
function testKeySizeLimitsRSA() {
  var e = [0x1, 0x00, 0x01];

  // A 1019 bit modulus.
  var n1019 = [0x04, 0x00, 0xb0, 0x44, 0x81, 0x6f, 0xd9, 0x7f,
               0x44, 0x3e, 0x30, 0x48, 0xc7, 0x75, 0xea, 0x83,
               0x44, 0xa1, 0xfa, 0xee, 0x42, 0x5c, 0x5b, 0x50,
               0x6a, 0xb2, 0x1b, 0x52, 0x58, 0x93, 0xaa, 0x76,
               0x2a, 0x5f, 0x32, 0xe1, 0x52, 0xaa, 0x7e, 0x80,
               0xf6, 0xb2, 0x2d, 0x50, 0x79, 0x3d, 0xf6, 0x26,
               0x44, 0x99, 0xa0, 0x99, 0x3a, 0x9d, 0xb0, 0x96,
               0x1a, 0xa7, 0xd5, 0xf5, 0xf2, 0x5b, 0x41, 0x94,
               0x09, 0xd6, 0x34, 0xdf, 0xb5, 0x28, 0x96, 0xf8,
               0x9c, 0x48, 0x4a, 0xbd, 0xa5, 0xc7, 0x40, 0x89,
               0x0b, 0x9e, 0x1d, 0x31, 0xe4, 0x7d, 0x49, 0x10,
               0xb6, 0x25, 0xd5, 0x5f, 0x44, 0xfd, 0xfc, 0xb6,
               0x55, 0x4c, 0x83, 0x8e, 0xfb, 0x56, 0x64, 0x66,
               0xd3, 0x49, 0x84, 0xa4, 0xdf, 0xc4, 0x88, 0x49,
               0x54, 0x2a, 0xdf, 0xb1, 0x4b, 0xaa, 0xa7, 0xce,
               0x5c, 0x14, 0x1d, 0x49, 0x86, 0xc8, 0x3b, 0x25];
  assertEquals(1019, (new e2e.BigNum(n1019)).getBitLength());
  var must_throw = assertThrows(function() {
    new e2e.cipher.Rsa(e2e.cipher.Algorithm.RSA, {n: n1019, e: e});
  });
  assertTrue(must_throw instanceof e2e.cipher.Error);

  // A 1020 bit modulus.
  var n1020 = [0x0a, 0x00, 0xbc, 0xcb, 0xab, 0x1d, 0x82, 0xcf,
               0xc2, 0x1a, 0xa4, 0x13, 0xd9, 0xb3, 0x0a, 0xcd,
               0xdb, 0xf6, 0x73, 0x54, 0x67, 0x43, 0x3d, 0xea,
               0xe5, 0x3d, 0x25, 0x95, 0x4a, 0x07, 0x1d, 0x4f,
               0x78, 0x15, 0x2e, 0xc7, 0xb4, 0xc4, 0x6b, 0xd8,
               0xa0, 0x41, 0x83, 0x50, 0xd5, 0x0b, 0xb0, 0x25,
               0x23, 0xf1, 0xd1, 0x96, 0x2f, 0x0d, 0x77, 0x18,
               0x1d, 0x66, 0x5e, 0x1d, 0x48, 0xb7, 0x3d, 0xcf,
               0x16, 0xa6, 0x91, 0xb8, 0xaa, 0x1b, 0xa0, 0x4c,
               0x22, 0xa3, 0x29, 0x31, 0xe2, 0x8a, 0x1c, 0xbf,
               0x75, 0x6b, 0xd7, 0x1c, 0x96, 0x2a, 0xe6, 0xc7,
               0x4c, 0x29, 0x50, 0x54, 0x90, 0x19, 0xed, 0x20,
               0xc8, 0xb6, 0x33, 0x6d, 0x53, 0xed, 0xa4, 0x24,
               0x4a, 0xea, 0xed, 0x33, 0x0c, 0x8b, 0x29, 0x94,
               0xf4, 0x86, 0x95, 0x66, 0x33, 0xaf, 0x15, 0x63,
               0x3c, 0xb9, 0xc7, 0x6f, 0x20, 0x8c, 0x60, 0x95];
  assertEquals(1020, (new e2e.BigNum(n1020)).getBitLength());
  var rsa = new e2e.cipher.Rsa(e2e.cipher.Algorithm.RSA, {n: n1020, e: e});
  assertTrue(rsa instanceof e2e.cipher.Rsa);
  assertEquals(1020, rsa.modulus.getBitLength());
};


function testBlinding() {
// vectors from ftp://ftp.rsa.com/pub/rsalabs/tmp/pkcs1v15crypt-vectors.txt
  var n = [0xa8, 0xb3, 0xb2, 0x84, 0xaf, 0x8e, 0xb5, 0x0b, 0x38, 0x70, 0x34,
           0xa8, 0x60, 0xf1, 0x46, 0xc4, 0x91, 0x9f, 0x31, 0x87, 0x63, 0xcd,
           0x6c, 0x55, 0x98, 0xc8, 0xae, 0x48, 0x11, 0xa1, 0xe0, 0xab, 0xc4,
           0xc7, 0xe0, 0xb0, 0x82, 0xd6, 0x93, 0xa5, 0xe7, 0xfc, 0xed, 0x67,
           0x5c, 0xf4, 0x66, 0x85, 0x12, 0x77, 0x2c, 0x0c, 0xbc, 0x64, 0xa7,
           0x42, 0xc6, 0xc6, 0x30, 0xf5, 0x33, 0xc8, 0xcc, 0x72, 0xf6, 0x2a,
           0xe8, 0x33, 0xc4, 0x0b, 0xf2, 0x58, 0x42, 0xe9, 0x84, 0xbb, 0x78,
           0xbd, 0xbf, 0x97, 0xc0, 0x10, 0x7d, 0x55, 0xbd, 0xb6, 0x62, 0xf5,
           0xc4, 0xe0, 0xfa, 0xb9, 0x84, 0x5c, 0xb5, 0x14, 0x8e, 0xf7, 0x39,
           0x2d, 0xd3, 0xaa, 0xff, 0x93, 0xae, 0x1e, 0x6b, 0x66, 0x7b, 0xb3,
           0xd4, 0x24, 0x76, 0x16, 0xd4, 0xf5, 0xba, 0x10, 0xd4, 0xcf, 0xd2,
           0x26, 0xde, 0x88, 0xd3, 0x9f, 0x16, 0xfb ];
  var e = [0x01, 0x00, 0x01];
  var d = [0x53, 0x33, 0x9c, 0xfd, 0xb7, 0x9f, 0xc8, 0x46, 0x6a, 0x65, 0x5c,
           0x73, 0x16, 0xac, 0xa8, 0x5c, 0x55, 0xfd, 0x8f, 0x6d, 0xd8, 0x98,
           0xfd, 0xaf, 0x11, 0x95, 0x17, 0xef, 0x4f, 0x52, 0xe8, 0xfd, 0x8e,
           0x25, 0x8d, 0xf9, 0x3f, 0xee, 0x18, 0x0f, 0xa0, 0xe4, 0xab, 0x29,
           0x69, 0x3c, 0xd8, 0x3b, 0x15, 0x2a, 0x55, 0x3d, 0x4a, 0xc4, 0xd1,
           0x81, 0x2b, 0x8b, 0x9f, 0xa5, 0xaf, 0x0e, 0x7f, 0x55, 0xfe, 0x73,
           0x04, 0xdf, 0x41, 0x57, 0x09, 0x26, 0xf3, 0x31, 0x1f, 0x15, 0xc4,
           0xd6, 0x5a, 0x73, 0x2c, 0x48, 0x31, 0x16, 0xee, 0x3d, 0x3d, 0x2d,
           0x0a, 0xf3, 0x54, 0x9a, 0xd9, 0xbf, 0x7c, 0xbf, 0xb7, 0x8a, 0xd8,
           0x84, 0xf8, 0x4d, 0x5b, 0xeb, 0x04, 0x72, 0x4d, 0xc7, 0x36, 0x9b,
           0x31, 0xde, 0xf3, 0x7d, 0x0c, 0xf5, 0x39, 0xe9, 0xcf, 0xcd, 0xd3,
           0xde, 0x65, 0x37, 0x29, 0xea, 0xd5, 0xd1];
  var p = [0xd3, 0x27, 0x37, 0xe7, 0x26, 0x7f, 0xfe, 0x13, 0x41, 0xb2, 0xd5,
           0xc0, 0xd1, 0x50, 0xa8, 0x1b, 0x58, 0x6f, 0xb3, 0x13, 0x2b, 0xed,
           0x2f, 0x8d, 0x52, 0x62, 0x86, 0x4a, 0x9c, 0xb9, 0xf3, 0x0a, 0xf3,
           0x8b, 0xe4, 0x48, 0x59, 0x8d, 0x41, 0x3a, 0x17, 0x2e, 0xfb, 0x80,
           0x2c, 0x21, 0xac, 0xf1, 0xc1, 0x1c, 0x52, 0x0c, 0x2f, 0x26, 0xa4,
           0x71, 0xdc, 0xad, 0x21, 0x2e, 0xac, 0x7c, 0xa3, 0x9d];
  var q = [0xcc, 0x88, 0x53, 0xd1, 0xd5, 0x4d, 0xa6, 0x30, 0xfa, 0xc0, 0x04,
           0xf4, 0x71, 0xf2, 0x81, 0xc7, 0xb8, 0x98, 0x2d, 0x82, 0x24, 0xa4,
           0x90, 0xed, 0xbe, 0xb3, 0x3d, 0x3e, 0x3d, 0x5c, 0xc9, 0x3c, 0x47,
           0x65, 0x70, 0x3d, 0x1d, 0xd7, 0x91, 0x64, 0x2f, 0x1f, 0x11, 0x6a,
           0x0d, 0xd8, 0x52, 0xbe, 0x24, 0x19, 0xb2, 0xaf, 0x72, 0xbf, 0xe9,
           0xa0, 0x30, 0xe8, 0x60, 0xb0, 0x28, 0x8b, 0x5d, 0x77];
  var plaintext = [0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,
                   0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,
                   0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02];
  var rsa = new e2e.cipher.Rsa(
    e2e.cipher.Algorithm.RSA,
    {n: n, e: e, d: d, p: p, q: q});
  assertTrue(rsa.use_blinding);
  var ciphertext = e2e.async.Result.getValue(rsa.encrypt(plaintext))['c'];
  var recovered = e2e.async.Result.getValue(rsa.decrypt({c:ciphertext}));
  assertArrayEquals(plaintext, recovered);
  // a few times more to make sure RSA blinding works consistently
  for (var i = 0; i < 10; i++) {
    var recovered = e2e.async.Result.getValue(rsa.decrypt({c:ciphertext}));
    assertArrayEquals(plaintext, recovered);
  }

};

//]]>
</script>
</head>
<body></body>
</html>
